package com.ymm.web.controller;

import com.ymm.driver.api.constants.Entity.NoticeEntity;
import com.ymm.driver.api.facade.DriverNoticeService;
import com.ymm.driver.api.utils.Result;
import com.ymm.driver.api.utils.ResultCodeEnum;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/notice")
public class DriverNoticeController {
    @Reference(version = "server")
    DriverNoticeService driverNoticeService;

    /**
     * 阅读一条通知
     *
     * @param noticeId  通知Id
     * @param driverId 司机Id
     * @return 阅读
     */
    @PostMapping("/read")
    public Result<NoticeEntity> readNotice(Long noticeId, Long driverId) {
        if (Objects.isNull(noticeId) || Objects.isNull(driverId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return driverNoticeService.readNotice(noticeId, driverId);
    }

    /**
     * 获取未读通知的数量
     *
     * @param driverId 司机Id
     * @return 未读通知的数量
     */
    @GetMapping("/countNoticeUnread")
    public Result<Integer> countNoticeUnread(Long driverId) {
        if (Objects.isNull(driverId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return driverNoticeService.countNoticeUnread(driverId);
    }

    /**
     * 根据通知类型统计未读数量
     *
     * @param driverId  司机Id
     * @param noticeType 通知类型
     * @return 对应类型未读通知的数量
     */
    @GetMapping("/countNoticeUnreadByNoticeType")
    public Result<Integer> countNoticeUnreadByNoticeType(Long driverId, Integer noticeType) {
        if (Objects.isNull(noticeType) || Objects.isNull(driverId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }

        return driverNoticeService.countNoticeUnreadByNoticeType(driverId, noticeType);
    }

    /**
     * 根据司机Id获取全部通知
     *
     * @param driverId 司机Id
     * @return 全部通知
     */
    @PostMapping("/listAllNotices")
    public Result<List<NoticeEntity>> listAllNotices(Long driverId) {
        if (Objects.isNull(driverId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }

        return driverNoticeService.listAllNotices(driverId);
    }

    /**
     * 根据司机Id和通知类型获取对应的通知
     *
     * @param driverId  司机Id
     * @param noticeType 通知类型
     * @return 对应通知列表
     */
    @PostMapping("/listNoticesByNoticeType")
    public Result<List<NoticeEntity>> listNoticesByNoticeType(Long driverId, Integer noticeType) {
        if (Objects.isNull(noticeType) || Objects.isNull(driverId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return driverNoticeService.listNoticesByNoticeType(driverId, noticeType);
    }

}
